package com.spring.lesson001.demo12.cglib.eg2;

import org.junit.Test;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author vhans
 */
public class CglibTest {
    @Test
    public void test2() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Service2.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("调用方法:" + method);
                return methodProxy.invokeSuper(o, objects);
            }
        });
        Service2 proxy = (Service2) enhancer.create();
        // m1()中使用m2()方法，两者都被拦截处理
        proxy.m1();
    }
}
